package com.orangelife.mobile.main.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.net.NetworkUtil;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.surrounding.activity.HotGoodsDetailActivity;
import com.thirdsdk.qiniu.QiniuHelper;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


public class HotGoodsListViewAdapter<T> extends FatherAdapter<T> {

    private Context context;
    private List<T> mlist;

    @Override
    public BaseAdapter setList(List<T> list) {
        this.mlist = list;
        return HotGoodsListViewAdapter.this;
    }

    public HotGoodsListViewAdapter(Context context, List<T> list) {
        this.context = context;
        this.mlist = list;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_hotgoods_double, null);
            holder.ivPhoto1 = (ImageView) convertView.findViewById(R.id.ivPhoto1);
            holder.tvGoodsName1 = (TextView) convertView.findViewById(R.id.tvGoodsName1);
            holder.tvStandard1 = (TextView) convertView.findViewById(R.id.tvStandard1);
            holder.tvPriceDiscount1 = (TextView) convertView.findViewById(R.id.tvPrice1);
            holder.tvPrice1 = (TextView) convertView.findViewById(R.id.tvPrice2);
            holder.llHotGoods1 = (LinearLayout) convertView.findViewById(R.id.llHotGoods1);


            holder.ivPhoto2 = (ImageView) convertView.findViewById(R.id.ivPhoto2);
            holder.tvGoodsName2 = (TextView) convertView.findViewById(R.id.tvGoodsName2);
            holder.tvStandard2 = (TextView) convertView.findViewById(R.id.tvStandard2);
            holder.tvPriceDiscount2 = (TextView) convertView.findViewById(R.id.tvPrice1_2);
            holder.tvPrice2 = (TextView) convertView.findViewById(R.id.tvPrice2_2);
            holder.llHotGoods2 = (LinearLayout) convertView.findViewById(R.id.llHotGoods2);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /* listview填数据 */
        List<Map<String, Object>> listmap = (List<Map<String, Object>>) mlist.get(position);
        if (listmap.size() > 1) {
            Map<String, Object> map = listmap.get(0);
            holder.tvGoodsName1.setText(String.valueOf(map.get("goodsName")));
            holder.tvStandard1.setText(String.valueOf(map.get("pdtDesc")));
            holder.tvPriceDiscount1.setText(Constant.RMB_FLAG + String.valueOf(map.get("price")));
            holder.tvPrice1.setText(Constant.RMB_FLAG + String.valueOf(map.get("mkPrice")));
            holder.tvPrice1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            ImageLoader.getInstance().displayImage(QiniuHelper.getUrlWithQiniuImageSize(String.valueOf(map.get("goodsLogo")), QiniuHelper.QiniuImageSize.IMAGE_SIZE_SMALL_PNG), holder.ivPhoto1);
            holder.llHotGoods1.setOnClickListener(new listener(map));

            Map<String, Object> map1 = listmap.get(1);

            holder.tvGoodsName2.setText(String.valueOf(map1.get("goodsName")));
            holder.tvStandard2.setText(String.valueOf(map1.get("pdtDesc")));
            holder.tvPriceDiscount2.setText(Constant.RMB_FLAG + String.valueOf(map1.get("price")));
            holder.tvPrice2.setText(Constant.RMB_FLAG + String.valueOf(map1.get("mkPrice")));
            holder.tvPrice2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            ImageLoader.getInstance().displayImage(QiniuHelper.getUrlWithQiniuImageSize(String.valueOf(map1.get("goodsLogo")), QiniuHelper.QiniuImageSize.IMAGE_SIZE_SMALL_PNG), holder.ivPhoto2);
            holder.llHotGoods2.setOnClickListener(new listener(map1));
            holder.llHotGoods2.setVisibility(View.VISIBLE);
        } else if (listmap.size() == 1) {
            Map<String, Object> map = listmap.get(0);
            holder.tvGoodsName1.setText(String.valueOf(map.get("goodsName")));
            holder.tvStandard1.setText(String.valueOf(map.get("pdtDesc")));
            holder.tvPriceDiscount1.setText(Constant.RMB_FLAG + String.valueOf(map.get("price")));
            holder.tvPrice1.setText(Constant.RMB_FLAG + String.valueOf(map.get("mkPrice")));
            holder.tvPrice1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            ImageLoader.getInstance().displayImage(QiniuHelper.getUrlWithQiniuImageSize(String.valueOf(map.get("goodsLogo")), QiniuHelper.QiniuImageSize.IMAGE_SIZE_SMALL_PNG), holder.ivPhoto1);
            holder.llHotGoods1.setOnClickListener(new listener(map));
            holder.llHotGoods2.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    class listener implements View.OnClickListener {

        private Map<String, Object> clikmap = null;

        public listener(Map<String, Object> map) {

            this.clikmap = map;
        }

        @Override
        public void onClick(View view) {

            if (!NetworkUtil.isConnectedNet(context)) {
                Toast.makeText(
                        context,
                        context.getResources().getString(
                                R.string.net_disconnected),
                        Toast.LENGTH_SHORT).show();
                return;
            }
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("detail", (Serializable) clikmap);
            intent.setClass(context, HotGoodsDetailActivity.class);
            intent.putExtras(bundle);
            context.startActivity(intent);

        }

    }


    class ViewHolder {
        /**
         * 照片
         */
        private ImageView ivPhoto1;
        /**
         * 标题
         */
        private TextView tvGoodsName1;
        /**
         * 重量
         */
        private TextView tvStandard1;
        /**
         * 折扣价
         */
        private TextView tvPriceDiscount1;
        /**
         * 现价
         */
        private TextView tvPrice1;
        /**
         * 点击事件
         */
        private LinearLayout llHotGoods1;


        /**
         * 照片
         */
        private ImageView ivPhoto2;
        /**
         * 标题
         */
        private TextView tvGoodsName2;
        /**
         * 重量
         */
        private TextView tvStandard2;
        /**
         * 折扣价
         */
        private TextView tvPriceDiscount2;
        /**
         * 现价
         */
        private TextView tvPrice2;
        /**
         * 点击事件
         */
        private LinearLayout llHotGoods2;


    }
}
